package com.developer.platform.controller.data;

import com.developer.platform.application.command.TemplateCmdManager;
import com.developer.platform.application.command.cmd.TemplateAdd;
import com.developer.platform.application.command.cmd.TemplateUpdate;
import com.developer.platform.application.query.TemplateQueryManager;
import com.developer.platform.application.query.dto.ResponseModel;
import com.developer.platform.application.query.dto.TemplateDto;
import com.developer.platform.controller.BaseController;
import com.veeker.core.bean.Pageable;
import com.veeker.log.annotations.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ：qiaoliang
 * @date ：2020-12-29
 */
@Api(tags = "模板管理")
@Validated
@RequestMapping("/data/template")
@RestController
public class TemplateController extends BaseController {

  private final TemplateQueryManager templateQueryManager;
  private final TemplateCmdManager templateCmdManager;

  public TemplateController(
      TemplateQueryManager templateQueryManager,
      TemplateCmdManager templateCmdManager) {
    this.templateQueryManager = templateQueryManager;
    this.templateCmdManager = templateCmdManager;
  }

  @Log(businessType = "模板管理",discription = "查询模板信息列表")
  @ApiOperation(value = "查询模板信息列表")
  @GetMapping
  public ResponseEntity<ResponseModel<TemplateDto>> list(Pageable pageable){
    return ok(templateQueryManager.page(pageable));
  }

  @Log(businessType = "模板管理",discription = "查询所有模板-供条件类搜索")
  @ApiOperation(value = "查询所有模板")
  @GetMapping("/search")
  public ResponseEntity<List<TemplateDto>> search(){
    return ok(templateQueryManager.page(new Pageable(999,1,null)).getList());
  }

  @Log(businessType = "模板管理",discription = "新增模板信息")
  @ApiOperation(value = "新增模板信息")
  @PostMapping
  public ResponseEntity<?> save(@Valid @RequestBody TemplateAdd templateAdd){
    templateCmdManager.save(templateAdd);
    return ResponseEntity.status(HttpStatus.CREATED).build();
  }

  @Log(businessType = "模板管理",discription = "修改模板信息")
  @ApiOperation(value = "修改模板信息")
  @PutMapping("/{id}")
  public ResponseEntity<?> update(@PathVariable("id") long id ,@Valid @RequestBody TemplateUpdate templateUpdate){
    templateCmdManager.update(id, templateUpdate);
    return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
  }

  @Log(businessType = "模板管理",discription = "删除模板信息")
  @ApiOperation(value = "删除模板信息")
  @DeleteMapping("/{id}")
  public ResponseEntity<?> delete(@PathVariable("id") long id){
    templateCmdManager.delete(id);
    return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
  }

}
